package com.klouda.platakybeta;

import com.klouda.platakybeta.db.PlatakyDbHandler;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LekEditActivity extends PlatakyActivity implements OnClickListener {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lek_edit_layout);
    m_nazev = (EditText) findViewById(R.id.nazevLeku);
    m_cena = (EditText) findViewById(R.id.cenaLeku);
    m_ulozit = (Button) findViewById(R.id.ulozitLek); 
    m_smazat = (Button) findViewById(R.id.smazatLek);    
    m_ulozit.setOnClickListener(this);
    m_smazat.setOnClickListener(this);
    
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.activity_lek, menu);
    return true;
  }

  @Override
  public void onClick(View p_view) {
    PlatakyDbHandler l_dbHandler = new PlatakyDbHandler(LekEditActivity.this);;
    boolean l_sqlOk = false;
    switch (p_view.getId()) {
    case R.id.ulozitLek:
      try {
        String l_nazev =  m_nazev.getText().toString();
        String l_cenaText = m_cena.getText().toString();
        float l_cena = Float.valueOf(l_cenaText);
        l_sqlOk = l_dbHandler.createLek(l_nazev, l_cena);
      } catch (Exception l_ex) {        
        l_ex.printStackTrace();
        l_sqlOk = false;
      }
      if (!l_sqlOk) {
        showErrorDialog(R.string.error, R.string.error_sql_insert);
      }
      break;

    case R.id.smazatLek:
      try {
        //!td dodelat mazani
        l_sqlOk = l_dbHandler.deleteLek(0);        
      } catch (Exception l_ex) {         
        l_ex.printStackTrace();
        l_sqlOk = false;
      }
      if (!l_sqlOk) {
        showErrorDialog(R.string.error, R.string.error_sql_delete);
      }
      break;
      
      
    default:
      break;
    }
    
  }
  
  private Button m_ulozit, m_smazat;
  private EditText m_nazev, m_cena;

}
